﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;
using Nizm0.QOT.WCFServiceLibrary;

namespace Nizm0.QOT.WCFService
{
    public partial class QOTStorageWCFService : ServiceBase
    {
        internal static ServiceHost myServiceHost = null;

        public QOTStorageWCFService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            if (myServiceHost != null)
            {
                myServiceHost.Close();
            }
            myServiceHost = new ServiceHost(typeof(QOTStorageWCFService));
            myServiceHost.Open();

            try
            {
                System.IO.File.Create(@"C:\a\UsługaUruchomiona.txt");
                myServiceHost.Closing += new EventHandler(myServiceHost_Closing);
                System.IO.File.AppendAllText(@"C:\a\UsługaUruchomiona.txt", myServiceHost.BaseAddresses.ToString());
            }
            catch
            { }
        }

        void myServiceHost_Closing(object sender, EventArgs e)
        {
            try
            {
                System.IO.File.Create(@"C:\a\UsługaZamknięta.txt");
                System.IO.File.AppendAllText(@"C:\a\UsługaUruchomiona.txt", myServiceHost.BaseAddresses.ToString());
            }
            catch
            { }
        }

        protected override void OnStop()
        {
            if (myServiceHost != null)
            {
                myServiceHost.Close();
                myServiceHost = null;
            }

            try
            {
                System.IO.File.Create(@"C:\a\UsługaZamknięta.txt");
                System.IO.File.AppendAllText(@"C:\a\UsługaZamknięta.txt", myServiceHost.BaseAddresses.ToString());
            }
            catch
            { }
        }
    }
}
